import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/home/user_detail_entity.dart';
import 'package:sports_new/server/api/home_api.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';

import '../../resources/colours.dart';

enum ListType { all, article, video, post, soccer, basket, subscribe ,planForm}

class UserPageLogic extends GetxController with GetTickerProviderStateMixin {
  UserDetailEntity? data;
  List<ExpertLeagueItem>? leagues;
  String id = Get.arguments;
  //1用户 2专家
  int type = 1;
  //初始tab
  String? index;
  int _tabIndex = 0;
  TabController? tabC;
  List<String> tabs = [];
  List<ListType> listType = [];
  final scrollC = ApmScrollController();
  bool showTop = false;
  bool _showFocusMore = false;

  int get sportsId {
    int num = (data?.isFoot ?? 0) - (data?.isBasket ?? 0);
    if(data?.isFoot == 0 && data?.isBasket == 0) return 0;
    if(num == 1 || num == 0){
      return 1;
    }else if(num == -1){
      return 2;
    }else{
      return 0;
    }
  }
  int get tabIndex => _tabIndex;
  bool get showFocusMore => _showFocusMore;

  set showFocusMore(bool value) {
    _showFocusMore = value;
    update();
  }
  set tabIndex(int value) {
    _tabIndex = value;
    update();
  }

  @override
  void onInit() {
    super.onInit();
    if (Get.parameters['type'] != null) {
      type = int.tryParse(Get.parameters['type']!) ?? 1;
    }
    if (Get.parameters['index'] != null && Get.parameters['index'] != "") {
      index = Get.parameters['index'];
    }
    scrollC.addListener(() {
      if (scrollC.offset > 160 && showTop == false) {
        showTop = true;
        update();
      } else if (scrollC.offset < 160 && showTop == true) {
        showTop = false;
        update();
      }
    });
  }

  @override
  void onReady() async {
    super.onReady();
    getData();
  }

  getData({bool isNew = true}) async {
    // if (type == 1 && User.isLogin) {
    //   UserApi.getUserPageSub(id).then((value) {
    //     userSub = value;
    //     update();
    //   });
    // }
    final result = await HomeApi.getUserDetail(id, type);
    if (result != null) {
      data = result;
      leagues = data?.leagueTop?.map((e) => ExpertLeagueItem.from(data,e)).toList() ?? [];
      if (isNew) {
        getTab();
      }
      update();
    }
  }

  void getTab() {
    tabs.clear();
    listType.clear();
    List<String> tabString = [];
    if(data?.isArticle == 1) {
      tabs.add('文章');
      listType.add(ListType.article);
      tabString.add('wenzhang');
    }
    if(data?.isVideo == 1) {
      tabs.add('视频');
      listType.add(ListType.video);
      tabString.add('shiping');
    }
    if(data?.isPost == 1) {
      tabs.add('帖子');
      listType.add(ListType.post);
      tabString.add('tiezi');
    }
    if(data?.isFoot == 1) {
      tabs.add('足球专栏');
      listType.add(ListType.soccer);
      tabString.add('zq');
    }
    if(data?.isBasket == 1) {
      tabs.add('篮球专栏');
      listType.add(ListType.basket);
      tabString.add('lq');
    }
    if(data?.isIdea == 1) {
      tabs.add('订阅');
      listType.add(ListType.subscribe);
      tabString.add('dingyue');
    }
    if(data?.isPlanList == 1) {
      tabs.add("足球计划单");
      listType.add(ListType.planForm);
      tabString.add("jihuadan");
    }
    if(index != null) {
      tabIndex = tabString.indexOf(index!);
      if (tabIndex == -1) tabIndex = 0;
    }
    tabC = TabController(
        length: tabs.length, vsync: this, initialIndex: tabIndex);
    tabC?.addListener(() {
      tabIndex = tabC?.index ?? 0;
    });
  }

  String filterText(String text) {
    String tag = '</br>';
    while (text.contains('</br>')) {
      text = text.replaceAll(tag, '\n\n');
    }
    return text;
  }

  double textHeight() {
    var text = utf8.decode(
        utf8.encode(data?.introduction ?? "该专家还未填写简介"), allowMalformed: true);
    TextPainter painter = TextPainter(
        textDirection: TextDirection.ltr,
        locale: const Locale('zh', 'CN'),
        textWidthBasis: TextWidthBasis.longestLine,
        maxLines: 3,
        text: TextSpan(
            text: filterText(text),
            style: const TextStyle(fontSize: 12, color: Colours.white)));
    painter.layout(maxWidth: Get.width-40);
    return painter.height;
  }

}
